<template>
  <div>
    <div style="display: flex;align-items: center;flex-wrap: wrap;">
      <div class="divItem">
        <el-input v-model.number="inputBiopsyHistoryId" placeholder="请输入扫描批次号" class="picker-form" size="mini"
        type="number"></el-input>
      </div>
      <div class="divItem">
        <el-input v-model="pathologyId" placeholder="请输入病理编号" class="picker-form" size="mini"></el-input>
      </div>
      <div class="divItem">
        <el-date-picker size="mini" style="max-width: 310px" v-model="createdRange" type="datetimerange"
        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </div>
      <div class="divItem">
        <el-select v-model="filterForm.slicePurpose" placeholder="请选择切片用途" clearable class="picker-form" size="mini">
          <el-option v-for="item in sliceUsesList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </div>
      <div class="divItem">
        <el-select v-model="filterForm.sliceHospital" class="picker-form" size="mini" @change="getBranchList"
        placeholder="请选择医院">
        <el-option v-for="t in hospitalList" :key="t.id" :label="t.name" :value="t.id">
        </el-option>
      </el-select>
      </div>
      <div class="divItem">
        <el-select v-model="filterForm.branchName" placeholder="请选择分院" size="mini">
          <el-option v-for="item in branchCourtsList" :key="item.name" :label="item.name" :value="item.name"></el-option>
        </el-select>
      </div>
      <div style="display: flex;align-items: center;flex-wrap: wrap;" class="divItem">
        <el-button @click="batchModifyDialog" type="primary" size="mini" >批量修改</el-button>
        <el-button @click="batchRescan" type="primary" size="mini">重新分析</el-button>
        <el-button @click="searchByBiopsyHistoryId" type="primary" size="mini">搜索</el-button>
      </div>

    </div>
  
    <el-row style="margin-top: 10px">
      <base-table ref="biopsyTable" :selected="isSelected" :whichPageHeader="scanMatchedHeader" :data="biopsyTable.data"
        :header="biopsyTable.header" :operation="biopsyTable.operation" :pageInfo="biopsyTable.pageInfo"
        :cellStyleFunc="diagnosisTableCellStyleFunc" @handleClick="biopsyTableButtonClick"
        @pageJumpTo="biopsyTablePageJumpTo" @sizeChange="biopsyTableSizeChange" @selectionChange="selectionChange"
        @handleSelectAll="handleSelectAll" tooltip-effect="“dark”">
        <template #nameSlot>
          <span>蓝色:切片不足;黑色:切片正常;红色:切片过多;</span>
        </template>
      </base-table>
    </el-row>
    <el-dialog :close-on-click-modal="false" title="诊断详情" :visible.sync="diagnosisDialog.visible" width="80%">
      <el-row style="margin-top: 10px">
        <el-col :span="10" :offset="12">
          <span>诊断状态：{{ diagnosis.currentLevelLabel }}</span>
          <div class="image-box" ref="IMAGE_SERVICE" @click="imagePinwheel($event)">
            <img :src="imageServiceURL + labelUrl" title="鼠标左键点击可顺时针旋转90度" style="width: 350px; height: 110px"
              alt="切片标签" />
          </div>
        </el-col>
      </el-row>
      <diagnosis-detail ref="diagnosisDetail" style="margin-top: 10px" :diagnosis="diagnosis"
        :isAdvancedSetting="isAdvancedSetting"></diagnosis-detail>
      <span slot="footer" class="dialog-footer">
        <el-button @click="diagnosisDialogSave" type="primary" size="mini">保存</el-button>
        <el-button @click="diagnosisDialog.visible = false" size="mini">关闭</el-button>
      </span>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" title="切片详情" :visible.sync="biopsyDetailDialog.visible" width="40%">
      <el-row style="margin-top: 10px" :gutter="20">
        <el-col :span="14">
          <el-row>
            <el-col :span="12">
              <span>切片编号：{{ biopsyDetail.id }}</span>
            </el-col>
            <el-col :span="12">
              <span>扫描倍率：{{ biopsyDetail.level }}</span>
            </el-col>
          </el-row>
          <el-row style="margin-top: 10px">
            <el-form label-width="85px" label-position="left">
              <el-form-item label="切片描述：">
                <el-input type="textarea" placeholder="请输入扫描切片描述" :autosize="{ minRows: 2, maxRows: 10 }"
                  v-model="biopsyDetail.description" size="mini"></el-input>
              </el-form-item>
            </el-form>
          </el-row>
        </el-col>
        <el-col :span="10" style="display: flex; align-items: center">
          <img :src="imageServiceURL + biopsyDetail.labelUrl" class="biopsy-detail-img" alt="切片标签" />
          <img :src="imageServiceURL + biopsyDetail.previewUrl" class="biopsy-detail-img" alt="切片缩略图" />
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="biopsyDetailDialogSave" type="primary" size="mini">保存</el-button>
        <el-button @click="biopsyDetailDialog.visible = false" size="mini">关闭</el-button>
      </span>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" title="批量修改" :visible.sync="isVisibleBatchModify" width="40%">
      <el-row :gutter="20" type="flex" justify="center">
        <el-form ref="refForm" :rules="formRules" :model="batchModifyForm">
          <el-col :span="12" class="batch-modify-col">
            <el-form-item label="检查类型" prop="checkCategory">
              <el-select class="picker-form" v-model="batchModifyForm.checkCategory" placeholder="请选择检查类型">
                <el-option v-for="(data, index) in checkCategoryList" :key="index" :label="data.name" :value="data.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="batch-modify-col">
            <el-form-item label="冰冻类型" prop="freezeType">
              <el-select v-model="batchModifyForm.freezeType" placeholder="请选择冰冻类型" class="picker-form">
                <el-option v-for="(data, index) in freezeTypeList" :key="index" :label="data.name" :value="data.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="batch-modify-col">
            <el-form-item label="取材脏器" prop="sampleOrganId">
              <el-select class="picker-form" v-model="batchModifyForm.sampleOrganId" placeholder="请选择取材脏器">
                <el-option v-for="(data, index) in organList" :key="index" :label="data.name" :value="data.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="batch-modify-col">
            <el-form-item label="标本类型" prop="specimenType">
              <el-select class="picker-form" v-model="batchModifyForm.specimenType" placeholder="请选择标本类型">
                <el-option v-for="(data, index) in specimenCategoryList" :key="index" :label="data.name"
                  :value="data.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="batch-modify-col">
            <el-form-item label="诊断组" prop="subspecialtyType">
              <el-select class="picker-form" v-model="batchModifyForm.subspecialtyType" placeholder="请选择诊断组">
                <el-option v-for="data in subspecialtyTypeList" :key="data.id" :label="data.name" :value="data.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="batch-modify-col">
            <el-form-item label="取材部位" prop="sampleBody" class="sample-body-padding">
              <el-input class="picker-form" placeholder="请选择取材部位" v-model="batchModifyForm.sampleBody" />
            </el-form-item>
          </el-col>
          <el-col :span="12" class="batch-modify-col">
            <el-form-item label="切片用途" prop="slicePurpose">
              <el-select v-model="batchModifyForm.slicePurpose" placeholder="请选择切片用途" class="picker-form">
                <el-option v-for="item in sliceUsesList" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="batch-modify-col">
            <el-form-item label="所属医院" prop="sliceHospital">
              <el-select v-model="batchModifyForm.sliceHospital" placeholder="请选择切片所属医院" class="picker-form">
                <el-option v-for="item in hospitalList" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="batchModifyClick" type="primary" size="mini">修改</el-button>
        <el-button @click="isVisibleBatchModify = false" size="mini">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import _ from 'lodash';
import BaseTable from '@templates/components/BaseTable';
import DiagnosisDetail from '@templates/components/DiagnosisMatchDetail';
import diagnosisDefault from '@templates/diagnosisDefault';
import { itemsPerPage } from '@/client/libs/util';
import moment from 'moment/moment';
export default {
  name: 'DiagnosisScanMatched',
  components: {
    DiagnosisDetail,
    BaseTable,
  },
  data() {
    return {
      currentDeg: 0,
      formRules: {
        checkCategory: [
          {
            required: true,
            message: '检查类型不能为空',
            trigger: 'change',
          },
        ],
        freezeType: [
          {
            required: true,
            message: '冰冻类型不能为空',
            trigger: 'change',
          },
        ],
        // sampleBody: [
        //     {
        //         required: true,
        //         message: '取材部位不能为空',
        //         trigger: 'blur',
        //     },
        // ],
        sampleOrganId: [
          {
            required: true,
            message: '取材脏器不能为空',
            trigger: 'change',
          },
        ],
        specimenType: [
          {
            required: true,
            message: '标本类别不能为空',
            trigger: 'change',
          },
        ],
        subspecialtyType: [
          {
            required: true,
            message: '诊断组不能为空',
            trigger: 'change',
          },
        ],
        sliceHospital: [
          {
            required: true,
            message: '切片所属医院不能为空',
            trigger: 'change',
          },
        ],
        slicePurpose: [
          {
            required: true,
            message: '切片用途不能为空',
            trigger: 'change',
          },
        ],
      },
      biopsyIds: [],
      isSelected: true, //是否支持多选
      selectedList: [], //批量修改保存diagnosisIds
      isVisibleBatchModify: false, //批量修改dialog显示框,
      dictRequestMap: [
        {
          code: 'checkCategory',
          arrayKey: 'checkCategoryList',
        },
        {
          code: 'specimenCategory',
          arrayKey: 'specimenCategoryList',
        },
        {
          code: 'freezeType',
          arrayKey: 'freezeTypeList',
        },
        {
          code: 'sliceUses',
          arrayKey: 'sliceUsesList',
        },
        {
          code: 'hospitalList',
          arrayKey: 'hospitalList',
        },
      ],
      organList: [], //取材脏器字典
      checkCategoryList: [], //检查类型字典
      specimenCategoryList: [], //标本类型字典
      freezeTypeList: [], //冰冻类型字典
      subspecialtyTypeList: [], //诊断字典
      batchModifyForm: {
        checkCategory: '',
        diagnosisIds: [],
        freezeType: '',
        sampleBody: '',
        sampleOrganId: '',
        specimenType: '',
        subspecialtyType: '',
        sliceHospital: '',
        slicePurpose: '',
      },
      copyBatchModifyForm: {},
      scanMatchedHeader: 'scanMatchedHeader', //从哪个页面引用的baseTable组件
      imageServiceURL: process.env.ImageServiceURL, // 图片服务器地址
      labelUrl: '', // 切片标签
      inputBiopsyHistoryId: '',
      createdRange: '',
      searchBiopsyHistoryId: '',
      pathologyId: '',
      biopsyTable: {
        data: [],
        header: [
          {
            label: '分院名称',
            prop: 'branchName',
            width: '107',
          },
          {
            label: '切片标签',
            width: '140',
            prop: 'labelUrl',
            isRender: true,
            render: (h, params) => {
              return h('img', {
                attrs: {
                  src: this.imageServiceURL + params.row.labelUrl,
                },
                style: {
                  width: '140px',
                  height: '65px',
                },
              });
            },
          },
          {
            label: '扫描批次',
            prop: 'biopsyHistoryId',
            width: '107',
          },
          {
            label: '扫描日期',
            prop: 'biopsyHistoryDate',
            width: '155',
          },
          {
            label: '切片类别',
            prop: 'sliceCategoryName',
            width: '107',
          },
          {
            label: '切片用途',
            prop: 'slicePurposeName',
            width: '107',
          },
          {
            label: '病理编号',
            prop: 'pathologyId',
            width: '107',
          },
          {
            label: 'AI分析结果',
            prop: 'isAi',
            width: '107',
          },
          {
            label: '已扫/总',
            prop: 'biopsyCheck',
            width: '100',
            tooltip: {
              name: 'nameSlot',
              effect: 'dark',
              placement: 'bottom',
            },
          },
          {
            label: '标本类别',
            prop: 'specimenTypeName',
            width: '107',
          },
          {
            label: '检查类型',
            prop: 'checkCategoryName',
            width: '107',
          },
          {
            label: '冰冻',
            prop: 'freezeTypeName',
            width: '107',
          },
          {
            label: '诊断组',
            prop: 'subspecialtyTypeName',
            width: '107',
          },
          {
            label: '取材脏器',
            prop: 'sampleOrganName',
            width: '107',
          },
          {
            label: '切片名称',
            prop: 'originFilename',
            width: '107',
          },
          {
            label: '姓名',
            prop: 'name',
            width: '107',
          },
          {
            label: '年龄',
            prop: 'age',
            width: '107',
          },
          {
            label: '性别',
            prop: 'sex',
            width: '107',
          },
          {
            label: '病区',
            prop: 'wardName',
            width: '107',
          },
          {
            label: '床号',
            prop: 'bedId',
            width: '107',
          },
          {
            label: '送检科室',
            prop: 'sendDepartment',
            width: '107',
          },
          {
            label: '取材部位',
            prop: 'sampleBody',
            width: '107',
          },
          {
            label: '上传时间',
            prop: 'createTime',
            width: '107',
          },
          {
            label: '匹配时间',
            prop: 'matchCreateTime',
            width: '107',
          },
          {
            label: '扫描倍率',
            prop: 'level',
            width: '107',
          },
          {
            label: '是否补传',
            prop: 'isAddedBiopsyName',
            width: '107',
          },
          {
            label: '匹配状态',
            prop: 'matchedStatusName',
            width: '107',
          },
          {
            label: '上传进度',
            prop: 'progress',
            width: '107',
          },
          {
            label: '切片所属医院',
            prop: 'sliceHospitalName',
            width: '120',
          },
          {
            label: '描述',
            prop: 'description',
          },
        ],
        operation: [
          {
            label: '编辑',
            functionKey: 'biopsyTableEdit',
          },
          {
            label: '切片描述',
            functionKey: 'biopsyTableDesc',
          },
          {
            label: '取消匹配',
            functionKey: 'cancletMatch',
          },
        ],
        // 分页信息
        pageInfo: {
          size: itemsPerPage.get(),
          page: 1,
          total: 0,
        },
      },
      biopsyDetailDialog: {
        visible: false,
      },
      biopsyDetail: {
        id: 0,
        level: '0',
        description: '',
        previewUrl: '',
        labelUrl: '',
      },

      diagnosisDialog: {
        visible: false,
        pathologyId: '',
      },

      diagnosis: diagnosisDefault,
      isAdvancedSetting: false,
      //切片用途list
      sliceUsesList: [],
      //切片所属医院列表
      hospitalList: [],
      branchCourtsList: [], //分院列表
      filterForm: {
        slicePurpose: '',
        sampleOrganId: '',
        branchId: '',
      },
    };
  },
  mounted() {
    this.biopsyTableRequest();
    this.dictionariesRequest();
  },

  methods: {
    //获取分院列表
    getBranchList(id) {
      this.branchCourtsList = []
      let mData = this.hospitalList.filter((item) => item.id === id)[0]
      if (mData.firstCode) {
        mData.firstCode.split('、').forEach((item) => {
          this.branchCourtsList.push({ name: item })
        })
      }
    },
    // 图片旋转
    imagePinwheel(e) {
      this.currentDeg += 90;
      this.$refs.IMAGE_SERVICE.style.transform = `rotate(${this.currentDeg}deg)`;
    },
    diagnosisTableCellStyleFunc({ row, column }) {
      const baseStyle = {
        padding: '3px 0',
        'border-right': '1px solid #ffffff',
        'font-size': '14px',
      };
      switch (column.label) {
        case '已扫/总':
          if (row.biopsyCheckColor == '#10a1df') {
            baseStyle.color = '#10a1df';
          } else if (row.biopsyCheckColor == 'red') {
            baseStyle.color = 'red';
          } else {
            baseStyle.color = '#666666';
          }
          break;
      }
      return baseStyle;
    },
    // 依据扫描批次号查询
    searchByBiopsyHistoryId() {
      this.searchBiopsyHistoryId = this.inputBiopsyHistoryId;
      this.biopsyTableRequest();
    },
    // 表格 - 按钮点击
    biopsyTableButtonClick(functionKey, row) {
      this[functionKey](row);
    },
    // TODO 表格 - 编辑
    biopsyTableEdit(row) {
      this.labelUrl = row.labelUrl;
      this.pBiopsyId = row.id;
      this.diagnosisDialogOpen(row.pathologyId);
    },
    // 表格 - 切片描述
    biopsyTableDesc(row) {
      this.biopsyDetailDialogOpen(row.index, row.pathologyId, row.id);
    },
    //取消匹配
    cancletMatch(row) {
      this.$confirm('是否确认取消?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.$axios({
          method: 'get',
          url: '/PROXY-Remote/biopsy/cancelMatch/' + row.id,
        }).then((res) => {
          // 请求失败
          if (res.data.code === 409) {
            this.$message.success(res.data.message);
          }
          // 请求成功
          else if (res.data.code === 200) {
            this.biopsyTable.data.forEach((item, index) => {
              if (item.id == row.id) {
                this.biopsyTable.data.splice(index, 1);
              }
            });
            this.$message.success('取消匹配成功');
            this.$emit('getAngelCount');
          } else {
            this.$message.success('退回失败，请联系管理员');
          }
        });
      });
    },
    // 切片详情
    async biopsyDetailDialogOpen(index, pathologyId, biopsyId) {
      Object.keys(this.biopsyDetail).forEach((key) => {
        this.biopsyDetail[key] = this.biopsyTable.data[index][key];
      });
      const res = await this.$axios({
        // 详见 axios 请求配置项
        method: 'GET',
        url: '/PROXY-Remote/diagnosis/single',
        params: {
          pathologyId,
        },
      });
      // 请求失败
      if (res.data.code !== 200 || !res.data.data) {
        throw new Error(res.data.code);
      }
      const biopsyItem = res.data.data.biopsyList.find((item) => {
        return item.id === biopsyId;
      });
      this.biopsyDetail.description = biopsyItem ? biopsyItem.description : '';
      this.biopsyDetailDialog.visible = true;
    },
    // 切片详情
    biopsyDetailDialogSave() {
      try {
        this.$axios({
          method: 'PUT',
          url: '/PROXY-Remote/biopsy',
          data: this.biopsyDetail,
        }).then((res) => {
          // 请求失败
          if (res.data.code !== 200) {
            throw new Error(res.data);
          }

          // 请求成功
          this.$message.success('保存成功');
          this.biopsyTableRequest();
          this.biopsyDetailDialog.visible = false;
        });
      } catch (e) {
        // 跳出
      }
    },
    // 分页跳转
    biopsyTablePageJumpTo(val) {
      this.biopsyTable.pageInfo.page = val;
      this.biopsyTableRequest();
    },
    // 每页显示数量改变
    biopsyTableSizeChange(val) {
      this.biopsyTable.pageInfo.size = val;
      this.biopsyTableRequest();
    },
    // 请求切片数据
    async biopsyTableRequest() {
      let _this = this;
      if (_this.createdRange && _this.createdRange.length > 0) {
        _this.filterForm.scanDateStart = moment(_this.createdRange[0])
          .utcOffset('+08:00')
          .format('YYYY-MM-DD HH:mm:ss');
        _this.filterForm.scanDateEnd = moment(_this.createdRange[1])
          .utcOffset('+08:00')
          .format('YYYY-MM-DD HH:mm:ss');
      } else {
        _this.filterForm.scanDateStart = '';
        _this.filterForm.scanDateEnd = '';
      }
      _this.filterForm.pathologyId = _this.pathologyId;
      _this.filterForm.biopsyHistoryid = _this.searchBiopsyHistoryId;
      _this.filterForm.page = _this.biopsyTable.pageInfo.page - 1;
      _this.filterForm.size = _this.biopsyTable.pageInfo.size;
      await _this
        .$axios({
          method: 'post',
          url: '/PROXY-Remote/biopsy/findAll/matchBiopsy',
          data: _this.filterForm,
        })
        .then((res) => {
          // 请求失败
          if (res.data.code !== 200) {
            throw new Error(res.data);
          }

          // 请求成功
          let result = res.data;
          // 查询结果数据
          _this.biopsyTable.data = [];
          res.data.data.forEach((t, index) => {
            let item = t;
            item.index = index;
            item.matchedStatusName =
              t.isManualMatch === 1
                ? '手动匹配'
                : t.isManualMatch === 0
                  ? '自动匹配'
                  : '';
            item.isAddedBiopsyName = t.isAddedBiopsy === 1 ? '是' : '否';
            item.biopsyHistoryDate = t.createTime;
            // moment(t.createTime).format('YYYY-MM-DD');
            item.progress = `${t.progress}%`;

            item.sex = t.sex === 283 ? '男' : t.sex === 284 ? '女' : '';

            let resultData = Object.assign({}, t.diagnosis, item);
            _this.biopsyTable.data.push(resultData);
          });

          // 分页信息
          _this.biopsyTable.pageInfo = {
            total: result.pageInfo.total,
            page: result.pageInfo.page + 1,
            size: result.pageInfo.size,
          };
          _this.addColor(_this.biopsyTable.data);
        });
    },
    //返回的数据给扫描确认加颜色
    addColor(shuju) {
      shuju.forEach((item) => {
        let biopsyChecks = item.biopsyCheck.split('/');
        item.biopsyCheckColor =
          Number(biopsyChecks[0]) < Number(biopsyChecks[1])
            ? '#10a1df'
            : Number(biopsyChecks[0]) > Number(biopsyChecks[1])
              ? 'red'
              : '#666666';
      });
    },
    diagnosisDialogOpen(pathologyId) {
      // 表单信息初始化
      this.diagnosis = _.clone(diagnosisDefault);
      this.diagnosisDialog.pathologyId = pathologyId;
      this.diagnosisRequest(pathologyId);
      this.diagnosisDialog.visible = true;
    },
    async diagnosisDialogSave() {
      const diagnosisData = _.clone(this.diagnosis);
      const { id } = this.diagnosis;
      const pBiopsyId = this.pBiopsyId;
      // 诊断三项高级设置处理
      if (!this.isAdvancedSetting) {
        delete diagnosisData.needFirstVisit;
        delete diagnosisData.needSkipSubmit;
        delete diagnosisData.canFinalVerify;
      }
      delete diagnosisData.biopsyList;
      delete diagnosisData.diagnosisDetailList;
      delete diagnosisData.sampleOrganOrgan;
      try {
        await this.$axios({
          // 详见 axios 请求配置项
          method: 'PUT',
          url: `/PROXY-Remote/diagnosis/updateDiagnosisForMatchBiopsy/${id}/${pBiopsyId}`,
          data: diagnosisData,
        }).then((res) => {
          // 请求失败
          if (res.data.code !== 200) {
            throw new Error(res.data);
          }

          // 请求成功
          // const result = res.data.data;
          this.$message.success('编辑成功!');
          this.biopsyTableRequest();
          this.diagnosisDialog.visible = false;
        });
      } catch (e) {
        // 跳出
      }
    },
    // 从数据库寻找诊断
    async diagnosisRequest(pathologyId) {
      const res = await this.$axios({
        // 详见 axios 请求配置项
        method: 'GET',
        url: '/PROXY-Remote/diagnosis/single',
        params: {
          pathologyId,
        },
      });
      // 请求失败
      if (res.data.code !== 200 || !res.data.data) {
        throw new Error(res.data.code);
      }

      const result = res.data.data;

      // 数据安全结构化
      result.cardId = result.cardId != null ? result.cardId : '';
      Object.keys(this.diagnosis).forEach((key) => {
        if (typeof this.diagnosis[key] === 'string') {
          this.diagnosis[key] = _.get(result, key, '');
        } else if (typeof this.diagnosis[key] === 'number') {
          this.diagnosis[key] = _.get(result, key, 0);
        } else if (this.diagnosis[key] instanceof Array) {
          this.diagnosis[key] = _.clone(result[key]);
        } else if (this.diagnosis[key] instanceof Object) {
          this.diagnosis[key] = _.get(result, key, { name: '' });
        } else {
          this.diagnosis[key] = _.get(result, key, '');
        }

        this.diagnosis.attachment = result.attachmentList;
        if (this.diagnosis[key] === null) {
          this.diagnosis[key] = '';
        }
      });
      //先兼容一下切片标签  后期要换接口
      this.diagnosis.sliceLabel = '';
      this.diagnosis.slicePurpose = this.diagnosis.biopsyList[0].slicePurpose; //切片用途默认切片列表的第一个切片用途

      this.diagnosis.allSliceLabel = ''; //总切片标签
      this.diagnosis.biopsyList.forEach((biopsy) => {
        if (biopsy.sliceLabel === null) {
          this.diagnosis.allSliceLabel = '';
        } else {
          this.diagnosis.allSliceLabel += biopsy.sliceLabel + ' 、';
        }
      });
      this.diagnosis.allsliceCategoryName = ''; //总切片类别
      this.diagnosis.biopsyList.forEach((biopsy) => {
        if (biopsy.sliceCategoryName === null) {
          this.diagnosis.allsliceCategoryName = '';
        } else {
          this.diagnosis.allsliceCategoryName +=
            biopsy.sliceCategoryName + ' 、';
        }
      });

      this.diagnosis.ifShowSetting = true;
    },
    selectionChange(selection) {
      this.selectedList = selection;
    },
    handleSelectAll(row) {
      this.selectedList = row;
    },
    async batchModifyDialog() {
      if (this.selectedList.length !== 0) {
        this.batchModifyForm.sampleBody = '';
        await this.dictionariesRequest();
        await this.organsRequest();
        await this.getSubGroupsDataList();
        this.isVisibleBatchModify = true;
        this.copyBatchModifyForm = JSON.parse(
          JSON.stringify(this.batchModifyForm)
        );
      } else {
        this.$message.warning('请选择要修改的切片');
      }
    },
    batchRescan() {
      if (this.selectedList.length !== 0) {
        this.$confirm('此操作AI将重新分析该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          this.biopsyIds = this.selectedList.map((item) => {
            return item.id;
          });
          let mData = { biopsyIds: [] };
          mData.biopsyIds = this.biopsyIds;
          this.$axios({
            // 详见 axios 请求配置项
            method: 'POST',
            url: '/biopsy/ai/againMatch',
            data: mData,
          }).then(async (res) => {
            if ((await res) && (await res.data.code) === 200) {
              await this.biopsyTableRequest();
              this.$message.success({
                message: '正在重新分析中......',
              });
            }
          });
        });
      } else {
        this.$message.warning('请选择要重新分析的切片');
      }
    },
    // 点击修改保存修改数据
    batchModifyClick() {
      if (this.validateForm()) {
        this.batchModifyForm.diagnosisIds = this.selectedList.map((item) => {
          return item.diagnosisId;
        });
        this.$axios({
          // 详见 axios 请求配置项
          method: 'POST',
          url: '/diagnosis/batch/diagnosisIds',
          data: this.batchModifyForm,
        }).then(async (res) => {
          if ((await res) && (await res.data.code) === 200) {
            this.isVisibleBatchModify = false;
            this.batchModifyForm = JSON.parse(
              JSON.stringify(this.copyBatchModifyForm)
            );
            await this.biopsyTableRequest();
            this.$message.success({ message: '批量修改成功' });
          }
        });
      }
    },
    // 字典数据请求
    async dictionariesRequest() {
      this.dictRequestMap.forEach((map) => {
        this.$axios({
          method: 'GET',
          url: '/PROXY-Remote/dicts/list',
          params: {
            code: map.code,
            page: 0,
            size: 99,
          },
        }).then(async (res) => {
          if ((await res) && (await res.data.code) === 200) {
            this[map.arrayKey] = _.clone(res.data.data);
          }
        });
      });
    },
    //取材脏器
    async organsRequest() {
      this.$axios({
        // 详见 axios 请求配置项
        method: 'GET',
        url: '/PROXY-Remote/organs',
        params: {
          size: 10000,
        },
      }).then(async (res) => {
        if ((await res) && (await res.data.code) === 200) {
          res.data.data.forEach((t) => {
            this.organList.push({
              id: t.id,
              name: t.name,
            });
          });
        }
      });
    },
    validateForm() {
      let decide = false;
      this.$refs.refForm.validate((valid) => {
        if (valid) {
          decide = true;
        } else {
          this.$message('请将必填信息填写完整');
        }
      });
      return decide;
    },
    // 诊断 列表获取
    async getSubGroupsDataList() {
      this.$axios({
        // 详见 axios 请求配置项
        method: 'GET',
        url: '/subgroups/searchSubgroupIdAndName',
      }).then(async (res) => {
        if ((await res) && (await res.data.code) === 200) {
          this.subspecialtyTypeList = res.data.data;
        }
      });
    },
  },
};
</script>

<style scoped >
.biopsy-detail-img {
  max-width: 100px;
  max-height: 100px;
  margin-left: 10px;
}

.image-box {
  /*display: flex;*/
  /*padding: 10px;*/
  /*justify-content: right;*/
  right: 20px;
  position: absolute;
  top: -70px;
}

.picker-form {
  width: 100%;
  max-width: 160px;
}

.batch-modify-col {
  padding-bottom: 16px;
}

.sample-body-padding {
  padding-left: 10px;
}
.divItem{
  margin-top:8px;
  margin-right: 8px;
}
</style>
